<?php
class Usuario extends CActiveRecord
{
	public $pass_repeat;
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre, pass, telefono, mail, ciudad', 'required'),
			array('telefono, aprobado', 'numerical', 'integerOnly'=>true),
			array('nombre, pass, mail', 'length', 'max'=>50),
			array('nombre, mail, telefono','unique'),
			array('pass', 'length', 'min'=>6),
			array('pass','compare'),
			array('pass_repeat','safe'), // para que la asigne tipo bulk ya que no est� en la DB
			array('id, nombre, pass, telefono, mail, aprobado', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'avisos' => array(self::HAS_MANY, 'Aviso', 'idUsuario'),
			'idLocalidad0' => array(self::BELONGS_TO, 'Localidad', 'idLocalidad'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nombre' => 'Nombre',
			'pass' => 'Clave',
			'pass_repeat' => 'Repita la Clave',
			'telefono' => 'Telefono',
			'mail' => 'Mail',
			'aprobado' => 'Aprobado',
			'idLocalidad' => 'Localidad',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('pass',$this->pass,true);
		$criteria->compare('telefono',$this->telefono);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('aprobado',$this->aprobado);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function validatePassword($password)
    {
        return $this->hashPassword($password,$this->salt)===$this->password;
    }
 
    public function hashPassword($password,$salt)
    {
        return md5($salt.$password);
    }
	
    public function getCiudad(){
            return $this->idLocalidad0->Provincia;
    }	
    
    public function getNombre(){
        return $this->nombre;
    }

}